using System;
using BitMango;
using BitMango.Diagnostics;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class BMConsent : MonoBehaviour
{
    private void OnEnable()
    {
        this.UpdateUIWithOrientation(Screen.orientation);
    }

    public static BMConsent Instance
    {
        get
        {
            if (BMConsent._instance == null)
            {
                BMConsent._instance = (BMConsent)UnityEngine.Object.FindObjectOfType(typeof(BMConsent));
                if (UnityEngine.Object.FindObjectsOfType(typeof(BMConsent)).Length > 1)
                {
                    return BMConsent._instance;
                }
                if (BMConsent._instance == null)
                {
                    BMConsent._instance = UnityEngine.Object.Instantiate<BMConsent>(Resources.Load<BMConsent>("BMConsent"));
                }
                //if (BMConsent._settingInstance == null)
                //{
                //    string companyName = "XLsoft";
                //    BMConsent._settingInstance = UnityEngine.Object.Instantiate<BMConsentSetting>(Resources.Load<BMConsentSetting>(companyName));
                //    Debugger.Assert(BMConsent._settingInstance.DidFillEverything(), "BMConsent : Fill everything!");
                //}
            }
            return BMConsent._instance;
        }
    }

    public static void Begin(Action callback)
    {
        Scene.Lock();
        BMConsent._callback = callback;
        if (EventSystem.current == null)
        {
            UnityEngine.Object.Instantiate<EventSystem>(Resources.Load<EventSystem>("EventSystem"));
        }
        ScreenOrientation orientation = Screen.orientation;

        BMConsent.Instance.FinishConsent();

        //if (Platform.IsTOSAgreed)
        //{
        //    BMConsent.Instance.FinishConsent();
        //}
        //else
        //{
        //    {
        //        BMConsent.Instance.ShowTOSWorld();
        //    }
        //}
    }

    public static void ShowOptOutAdsSetting(Action callback = null)
    {
        Scene.Lock();
        BMConsent._callback = callback;
        BMConsent.Instance.ShowTargetingAds();
    }

    private void ShowTOSEU(ScreenOrientation orientation)
    {
        if (this.consentEU == null)
        {
            string path = "ConsentEU_" + orientation.ToString();
            this.consentEU = UnityEngine.Object.Instantiate<ConsentEU>(Resources.Load<ConsentEU>(path), base.transform, false);
            this.consentEU.Initialize(BMConsent._settingInstance);
        }
        ConsentEU consentEU = this.consentEU;
        consentEU.OnDidClickStartPlaying = (Action)Delegate.Combine(consentEU.OnDidClickStartPlaying, new Action(this.OnDidClickStartPlayingEU));
        this.consentEU.Show();
    }

    private void ShowTOSKR(ScreenOrientation orientation)
    {
        if (this.consentKR == null)
        {
            string path = "ConsentKR_" + orientation.ToString();
            this.consentKR = UnityEngine.Object.Instantiate<ConsentKR>(Resources.Load<ConsentKR>(path), base.transform, false);
            this.consentKR.Initialize(BMConsent._settingInstance);
        }
        ConsentKR consentKR = this.consentKR;
        consentKR.OnDidClickStartPlaying = (Action)Delegate.Combine(consentKR.OnDidClickStartPlaying, new Action(this.OnDidClickStartPlayingKR));
        this.consentKR.Show();
    }

    private void ShowTOSWorld()
    {
        if (this.consentWorld == null)
        {
            this.consentWorld = UnityEngine.Object.Instantiate<ConsentWorld>(Resources.Load<ConsentWorld>("ConsentWorld"), base.transform, false);
            this.consentWorld.Initialize(BMConsent._settingInstance);
        }
        ConsentWorld consentWorld = this.consentWorld;
        consentWorld.OnDidClickStartPlaying = (Action)Delegate.Combine(consentWorld.OnDidClickStartPlaying, new Action(this.OnDidClickStartPlayingWorld));
        this.consentWorld.Show();
    }

    private void ShowTargetingAds()
    {
        if (this.consentTargetingAds == null)
        {
            this.consentTargetingAds = UnityEngine.Object.Instantiate<ConsentTargetingAds>(Resources.Load<ConsentTargetingAds>("ConsentTargetingAds"), base.transform, false);
            this.consentTargetingAds.Initialize(BMConsent._settingInstance);
        }
        this.consentTargetingAds.Show();
        ConsentTargetingAds consentTargetingAds = this.consentTargetingAds;
        consentTargetingAds.OnDidClickSave = (Action)Delegate.Combine(consentTargetingAds.OnDidClickSave, new Action(this.OnDidClickSaveTargetingAds));
    }

    private void OnDidClickStartPlayingEU()
    {
        ConsentEU consentEU = this.consentEU;
        consentEU.OnDidClickStartPlaying = (Action)Delegate.Remove(consentEU.OnDidClickStartPlaying, new Action(this.OnDidClickStartPlayingEU));
        this.consentEU.Hide();
        this.FinishConsent();
    }

    private void OnDidClickStartPlayingKR()
    {
        ConsentKR consentKR = this.consentKR;
        consentKR.OnDidClickStartPlaying = (Action)Delegate.Remove(consentKR.OnDidClickStartPlaying, new Action(this.OnDidClickStartPlayingKR));
        this.consentKR.Hide();
        this.FinishConsent();
    }

    private void OnDidClickStartPlayingWorld()
    {
        ConsentWorld consentWorld = this.consentWorld;
        consentWorld.OnDidClickStartPlaying = (Action)Delegate.Remove(consentWorld.OnDidClickStartPlaying, new Action(this.OnDidClickStartPlayingWorld));
        this.consentWorld.Hide();
        this.FinishConsent();
    }

    private void OnDidClickSaveTargetingAds()
    {
        ConsentTargetingAds consentTargetingAds = this.consentTargetingAds;
        consentTargetingAds.OnDidClickSave = (Action)Delegate.Remove(consentTargetingAds.OnDidClickSave, new Action(this.OnDidClickSaveTargetingAds));
        this.consentTargetingAds.Hide();
        this.FinishConsent();
    }

    private void FinishConsent()
    {
        Scene.Unlock();
        if (BMConsent._callback != null)
        {
            BMConsent._callback();
        }
    }

    private void UpdateUIWithOrientation(ScreenOrientation orientation)
    {
        CanvasScaler component = base.GetComponent<CanvasScaler>();
        if (orientation == ScreenOrientation.LandscapeLeft)
        {
            component.referenceResolution = new Vector2(1136f, 640f);
        }
        else
        {
            component.referenceResolution = new Vector2(640f, 1136f);
        }
    }

    private static Action _callback;

    private ConsentEU consentEU;

    private ConsentKR consentKR;

    private ConsentWorld consentWorld;

    private ConsentTargetingAds consentTargetingAds;

    private static BMConsent _instance;

    private static BMConsentSetting _settingInstance;
}
